var the_month;

function calendar_task_item(item){
//	console.log(item);
	var str='';
	str='<div id="ct'+item.id+'" data-taskid="'+item.id+'" data-stage="'+item.stage+'" class="task" draggable="true" ondragstart="calendar_task_dragstart(event)"><span class="task_title">'+item.title+'</span></div>';
//	console.log(str);
	return str;
}

function calendar_task_load(theday){
	if(!theday){
		theday="";
	}
	//如果页面设置了根任务，则提交根任务参数
	var rootTaskId = $("#parm").attr("data-rootTaskId");
	var postData = new Object;
	if(rootTaskId && rootTaskId!=1){
		postData['root_task']=rootTaskId;
	}
	if('undefined' !== typeof teamid){
		postData['teamid']=teamid;
	}
		var items;
//		$.getJSON("/index.php?c=taskcalendar&m=get_task_for_cal", 
		$.post("/index.php/taskcalendar/getmycal/" + theday, 
			postData,
			function(reponseData,status) { 
//				console.log(reponseData);
				if(items=reponseData.data){
//					items=reponseData.data;
						$.each(items,function(idx,item){ //生成所有任务，先挂装到指定日期下
//							console.log(item);
							$('td.fc-day[data-date="'+String(item.start).substring(0,10)+'"]').find('.fc-day-content').append(calendar_task_item(item));
						});
				}
			}		
		,"json");
}
//旧的，使用GET方式取数据，并且不使用根任务rootTask参数
function calendar_task_load0(theday){
	if(!theday){
		theday="";
	}
//var loading=layer.msg('正从服务器上加载数据，请稍候！', {icon: 16});
//		var loading = layer.load(2); //换了种风格

		var items;
//		$.getJSON("/index.php?c=taskcalendar&m=get_task_for_cal", 
		$.getJSON("/index.php/taskcalendar/getmycal/" + theday, 
			function(dataGet) { 
//			console.log(dataGet);
				if(dataGet){
//					console.log(dataGet.data);
					items=dataGet;
						$.each(items,function(idx,item){ //生成所有任务，先挂装到指定日期下
//							console.log(item);
							$('td.fc-day[data-date="'+String(item.start).substring(0,10)+'"]').find('.fc-day-content').append(calendar_task_item(item));
						});
				}
			}		
		);


}
function create_calendar(current_month){
	var month_firstday;
	var calendar_start;
	if(!current_month){	
//		console.log(moment().format("YYYY-MM-DD HH"));
		month_firstday = moment().startOf('month');
	}else{	
		month_firstday = moment(current_month+'-01');
	}
	
//	console.log(month_firstday.format("YYYY-MM-DD HH"));
//	console.log(month_firstday.format("YYYY-MM-DD"));
//	calendar_start = month_firstday.subtract((month_firstday.format("E")-1),'day');

//	console.log(calendar_start);
	var theday;
	var str='<div class="mycalendar fc fc-ltr fc-view fc-view-month fc-grid" data-month="' + month_firstday.format("YYYY-MM-DD") + '" >';
	str += '<table class="fc-header" style="width:100%">';
	str += '<tbody><tr><td class="fc-header-left">';
	str += '<span class="fc-button fc-button-prev fc-state-default fc-corner-left" unselectable="on" onclick="prev_month();"><span class="fc-text-arrow">‹</span></span><span class="fc-button fc-button-next fc-state-default fc-corner-right" unselectable="on" onclick="next_month();"><span class="fc-text-arrow">›</span></span><span class="fc-header-space"></span><span class="fc-button fc-button-today fc-state-default fc-corner-left fc-corner-right" onclick="back_current_month();" unselectable="on">当月</span></td>';
	str += '<td class="fc-header-center"><span class="fc-header-title"><h2>'+month_firstday.format("YYYY-MM")+'</h2></span></td>';
	str += '<td class="fc-header-right"></td></tr></tbody></table>';
//	console.log(str);
//	calendar_start = month_firstday.startOf('isoWeek');	
	theday = month_firstday.startOf('isoWeek');	
	str += '<table class="fc-border-separate" style="width:100%" cellspacing="0"><thead><tr class="fc-first fc-last"><th class="fc-day-header fc-mon fc-widget-header fc-first" style="width: 65px;">一</th><th class="fc-day-header fc-tue fc-widget-header" style="width: 65px;">二</th><th class="fc-day-header fc-wed fc-widget-header" style="width: 65px;">三</th><th class="fc-day-header fc-thu fc-widget-header" style="width: 65px;">四</th><th class="fc-day-header fc-fri fc-widget-header" style="width: 65px;">五</th><th class="fc-day-header fc-sat fc-widget-header" style="width: 65px;">六</th><th class="fc-day-header fc-sun fc-widget-header fc-last">日</th></tr></thead><tbody>';
	for (var i=0;i<6;i++)
	{
		str += '<tr class="fc-week">';
		for (var j=0;j<7;j++)
		{
//			console.log(theday.isBefore()?'last':'haft');
			
			str += '<td class="fc-day fc-week-'+j+(theday.isBefore(moment(),'day')?' foretime':' ')+' fc-widget-content" data-date="'+theday.format("YYYY-MM-DD")+'"  ondragover="calendar_dragover(event)" ondrop="calendar_drop(event)" ondragenter="calendar_dragenter(event)" ondragleave="calendar_dragleave(event)" >'
				+'<div><div class="fc-day-number">'+theday.format("D")+'</div>'
				+'<div class="fc-day-content"><div style="position: relative; height: 0px;">&nbsp;</div></div></div></td>';
			theday.add(1,'d');
		}
		str += '</tr>';
	}
	str += '</tbody></tabel>';
	str +='</div>';
	return str;
}

function append_calendar(new_month_str){
	$('#calendar_view .mycalendar').remove();
	$('#calendar_view').append(create_calendar(new_month_str));
//	$('td[data-date^='+moment().format('YYYY-MM-')+']').addClass('current_month');
	$('td[data-date^='+new_month_str+']').addClass('current_month');
	$('td[data-date='+moment().format('YYYY-MM-DD')+']').addClass('today');	
}

function prev_month(){
	var old_month_str = $('#calendar_view .mycalendar').attr("data-month");
	console.log(old_month_str);
	var new_month_str = moment(old_month_str).subtract(1,'months').format("YYYY-MM");
	append_calendar(new_month_str);
	calendar_task_load(new_month_str+'-01');
}
function next_month(){
	var old_month_str = $('#calendar_view .mycalendar').attr("data-month");
	console.log(old_month_str);
	var new_month_str = moment(old_month_str).add(1,'months').format("YYYY-MM");
	append_calendar(new_month_str);
	calendar_task_load(new_month_str+'-01');
}
function back_current_month(){
	var new_month_str = moment().format("YYYY-MM");
	append_calendar(new_month_str);
//	append_calendar('');
	calendar_task_load();
}

$(document).ready(function() {
	back_current_month();
	
	$("#calendar_view").delegate('.task','click',function(e){
		event = e||event; //为兼容firefox的事件模型
			var taskid=$(event.target).closest('.task').attr('data-taskid');
			position_task_id(taskid);
	});
	
});	//end of $(document).ready事件